package com.parking.dao.impl;

import com.parking.bean.ParkAdmin;
import com.parking.dao.ParkAdminDao;
import com.parking.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ParkAdminDaoImpl implements ParkAdminDao {
    @Override
    public List<ParkAdmin> list() {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet=null;
        List<ParkAdmin>list = new ArrayList<>();
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            String sql = "select * from park_admin ";
            ps = conn.prepareStatement(sql);
            /*执行Sql语句*/
            resultSet = ps.executeQuery();
            /*处理结果集*/
            while (resultSet.next()) {
                String id =resultSet.getString(1);
                String username =resultSet.getString(2);
                String password =resultSet.getString(3);
                String name =resultSet.getString(4);
                String gender =resultSet.getString(5);
                String telephone =resultSet.getString(6);
                list.add(new ParkAdmin(id,username,password,name,gender,telephone));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public List<ParkAdmin> search(String key, String value) {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet=null;
        List<ParkAdmin>list = new ArrayList<>();
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            /*String sql = "select * from park_admin where ? like '%'?'%'";*/
            String sql = "select * from park_admin where "+key+" like '%"+value+"%'";
            ps = conn.prepareStatement(sql);
            /*执行Sql语句*/
            resultSet = ps.executeQuery();
            /*处理结果集*/
            while (resultSet.next()) {
                String id =resultSet.getString(1);
                String username =resultSet.getString(2);
                String password =resultSet.getString(3);
                String name =resultSet.getString(4);
                String gender =resultSet.getString(5);
                String telephone =resultSet.getString(6);
                list.add(new ParkAdmin(id,username,password,name,gender,telephone));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public Integer save(ParkAdmin parkAdmin) {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        Integer result=null;
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            /*String sql = "select * from park_admin where ? like '%'?'%'";*/
            String sql = "insert into park_admin(username,password,name,gender,telephone) values(?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,parkAdmin.getUsername());
            ps.setString(2,parkAdmin.getPassword());
            ps.setString(3,parkAdmin.getName());
            ps.setString(4,parkAdmin.getGender());
            ps.setString(5,parkAdmin.getTelephone());
            /*执行Sql语句*/
            result = ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public Integer update(ParkAdmin parkAdmin) {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        Integer result=null;
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            /*String sql = "select * from park_admin where ? like '%'?'%'";*/
            String sql = "update park_admin set username=?,password=?,name=?,gender=?,telephone=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,parkAdmin.getUsername());
            ps.setString(2,parkAdmin.getPassword());
            ps.setString(3,parkAdmin.getName());
            ps.setString(4,parkAdmin.getGender());
            ps.setString(5,parkAdmin.getTelephone());
            ps.setString(6,parkAdmin.getId());
            /*执行Sql语句*/
            result = ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public Integer delete(String id) {
        /*链接数据库*/
        Connection conn = null;
        PreparedStatement ps = null;
        Integer result=null;
        try {
            /*获取链接*/
            conn = DBUtil.getConnection();
            /*获取预编译的数据库操作对象*/
            /*String sql = "select * from park_admin where ? like '%'?'%'";*/
            String sql = "delete from park_admin where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            /*执行Sql语句*/
            result = ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                /*释放资源*/
                DBUtil.close(conn, ps, null);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }
}
